package cn.ghb.eshop.common.config;

import cn.ghb.eshop.common.security.SysUserDetailsManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author Clown
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private final SysUserDetailsManager sysUserDetailsManager;
    private final PasswordEncoder passwordEncoder;

    @Autowired
    public WebSecurityConfig(SysUserDetailsManager sysUserDetailsManager, PasswordEncoder passwordEncoder) {
        this.sysUserDetailsManager = sysUserDetailsManager;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //关闭csrf
        http.csrf().disable();
        http.authorizeRequests()
                /*这个是排除的地址*/
                .antMatchers().permitAll()
                /*这个是所有的都要登录验证*/
                .anyRequest().authenticated();
        /*使用自带的登录页面*/
        http.formLogin();

        http.httpBasic();

        super.configure(http);
    }

}
